var pageTitle = '显示第{0}到第{1}条记录，共{2}条记录';

/**
 * 
 * @param dataList
 * @returns
 */
function selectAppend(
		items
	){
	var id = items['id'],dataList = items['data'],key = items['key'],value = items['value'],item = items['item'],cascade = items['cascade'];
	if(id){
		item = $('#'+id);
	}
	item.chosen();
	//是否有级联
	if(cascade){
		$("#"+id +" option").remove();
		$("#"+id).trigger("chosen:updated");
	}
	$.each(dataList,function(index,data){
		item.append('<option value="'+data[key]+'">'+
				data[value]+
		'</option>  ');
	});
	item.trigger("chosen:updated");
}
/**
 * 传输刷新分页的数据。只支持继承的分页
 * @param page
 * @returns
 */
function refreshPage(page){debugger
	$('.pageHelper').html('');
	$('.pageHelper').html('<div><span class="pageInfo"></span></div><div><ul class="pageLimit" ></ul></div>');
	page['currentPage'] = 1;
	initReqPage(page);
}
/**
 * 请求page分页
 * 传入
 * {
 * 		url:'请求地址',
 * 		useToken:'是否需要权限',
 * 		successFn:function(resp){}请求的方法。返回数据对象
 * 		errorMsgFn:function(resp){}数据异常
 * 		currentPage:当前第几页 非必填
 * 		numberOfPages:每页显示多少条 非必填
 * }
 */
function initReqPage(page){
	var defaultCurrentPage = 1,//默认页数
		defaultNumberOfPages = 5,//默认显示页数
		req = {},currentPage = page['currentPage'],numberOfPages = page['numberOfPages'],
		data = page.queryDataFn();
	if(!currentPage)
		currentPage = defaultCurrentPage;
	if(!numberOfPages)
		numberOfPages = defaultNumberOfPages;
	data['start'] = currentPage;
	data['rows'] = numberOfPages;
	req['reqJSON'] = {
		type : 'get',
		url: page['url'],
		data : data
	};
	req['useToken'] = page['useToken'];
	req['successFn'] = function(respData){
		if(respData.code == SUCCESS_CODE){
			var pageData = {};
			pageData['total'] = respData.data.totalSize;
			pageData['result'] = respData.data.result;
			pageData['currentPage'] = currentPage;
			pageData['numberOfPages'] = numberOfPages;
			pageData['successFn'] = page['successFn'];
			pageData['reqJSON'] = req['reqJSON'];
			pageData['queryDataFn'] = page['queryDataFn'];
			pageData['useToken'] = req['useToken'];
			initPage(pageData); 
		}else{
			page.errorMsgFn(respData);
		}
	};
	ajaxRequest(req);
}
function initPage(page){
	var total = page['total'],
		length = page['result'].length,//数据
		totalPage = total /length + (total % length != 0 ? 1: 0),//总页数
		currentPage = page['currentPage'],//第几页
		numberOfPages = page['numberOfPages'];//显示多少页
	if(length == 0){
		$('.pageInfo').html('没有搜索到相关记录');
		return;
	}
	var _startPage = (currentPage-1)*numberOfPages,
	_endPage = _startPage+length;
	if(totalPage  == 1){
		$('.pageInfo').html(
				pageTitle.
				replace('{0}',_startPage+1).
				replace('{1}',_endPage).
				replace('{2}',total));
//		 $('.pageLimit').bootstrapPaginator(options);
		 page.successFn(page.result);
		return;
	}
	var options = {
		bootstrapMajorVersion:3,
		alignment:"right",
		//	shouldShowPage:true,//是否显示该按钮
	    currentPage: currentPage, //	设置当前页.
	    totalPages: totalPage, //设置总页数.
		numberOfPages:numberOfPages, //	设置控件显示的页码数.即：类型为"page"的操作按钮的数量。
		itemTexts: function (type, page, current) {//设置显示的样式，默认是箭头
	        switch (type) {
	            case "first":
	                return "<<";
	            case "prev":
	                return "<";
	            case "next":
	                return ">";
	            case "last":
	                return ">>";
	            case "page":
	                return page;
	        }
	    }, 
	    onPageClicked: function (event, originalEvent, type, start) 
	    {
		    	var _startPage = (start-1)*numberOfPages,
				_endPage = _startPage+length;
		    	$('.pageInfo').html(
		    			pageTitle.
		    			replace('{0}',_startPage+1).
		    			replace('{1}',_endPage).
		    			replace('{2}',total));
		    	var req = {},queryData;
		    	req['reqJSON'] = page['reqJSON'];
		    	queryData = page.queryDataFn();
		    	queryData['start'] = start;
		    	queryData['rows'] = numberOfPages;

		    	req['reqJSON']['data'] = queryData;
		    	req['useToken'] = page['useToken'];
		    	req['successFn'] = function(respData){
		    		if(respData.code == SUCCESS_CODE){
		    			page.successFn(respData.data.result);
		    		}
	    		};
	    		
	    		ajaxRequest(req);
	    }
	}
	$('.pageInfo').html(
			pageTitle.
			replace('{0}',_startPage+1).
			replace('{1}',_endPage).
			replace('{2}',total));
	 $('.pageLimit').bootstrapPaginator(options);
	 page.successFn(page.result);
}

/**
 * 修改页面--》反选
 * @param id 页面下拉ID
 * @param data 显示数据
 * @param body 窗口对象
 * @param vlaue 页面的key值
 * @returns
 */
function updSelVal(items){
	var id = items['id'],body = items['body'],dataList = items['data'],value = items['value'],key = items['key'],name = items['name'];
	var idVal = body.find('#'+id);
	idVal.chosen();
	var item = {
			'item':idVal,
			'data':dataList,
			key:key ?key:'parmname',
			value:name ?name:'parmvalue'
		};
	selectAppend(item);
	idVal.val(value);
	idVal.trigger("chosen:updated");
}
function initSelectData(items){
	var meItem,id = items['id'],body = items['body'],typeKey = items['typeKey'],cascade = items['cascade'];
	meItem = body ? body : $('#'+id);
	meItem.chosen();
	items['key'] = 'key';
	items['value'] = 'value';
	selectAppend(items);
	if(items.successFn)
		items.successFn(meItem);
}
/**
 * 适用于List和add页面 初始化下拉  $("#tbParBudCode option[value='" + $("#hidBudCodeID").val() + "']").attr("disabled", "disabled"); 
 */
function initSelect(items){
	var meItem,id = items['id'],body = items['body'],typeKey = items['typeKey'],cascade = items['cascade'];
	meItem = body ? body : $('#'+id);
	meItem.chosen();
	var req = {};
	req['reqJSON'] = {
		type : 'get',
		url : BUSINESS_URL+'bs/gbdict/queryGbDictByType',
		data:{parmtype:typeKey}
	};
	req['useToken'] = true;
	req['successFn'] = function(respData){
		if(respData.code == SUCCESS_CODE){
			var dicData = respData.data;
			var item = {
				'id':id,
				'item':body,
				'data':dicData,
				key:'parmname',
				value:'parmvalue',
				cascade:cascade
			};
			selectAppend(item);
			if(items.successFn)
				items.successFn(dicData);
		}
	};
	ajaxRequest(req);
}

//初始化echarts折线图
function initEcharts(items){
	var dataStrArr=items['yData'];//分割成字符串数组  
    var dataIntArr=[];//保存转换后的整型字符串  
    //方法一  
    dataStrArr.forEach(function(data,index,arr){  
        dataIntArr.push(+data);  
    });
	// 基于准备好的dom，初始化echarts实例
	var myChart = echarts.init(document.getElementById(items['id']));
	var option1 = {
		title : {
		        text: items['xName'],
		        subtext: '间隔时间'+items['subtext']+'秒'
		},
		tooltip : { //提示框组件
			trigger : 'axis'
		},
		//grid x:距离左边多少距离
		//     x2:距离右边多少距离
		//     y:距离上边多少距离
		//     y2:距离下面边多少距离
        grid: {
        	x:'10%',
            x2: '5%',
            y: '20%',
            y2: '15%'
        },
        toolbox: {
            show : true,
            feature : {
                mark : {show: true},
                dataView : {show: true, readOnly: false},
                magicType : {show: true, type: ['line', 'bar']},
                restore : {show: true},
                saveAsImage : {show: true}
            }
        },
		xAxis : { //直角坐标系 grid 中的 x 轴
			type : 'category',
			//坐标轴两边留白策略，类目轴和非类目轴的设置和表现不一样。
			boundaryGap : false,
			data : items['xData'],
			axisLine:{
	        	show:false
	        },
            axisLabel : {
            	formatter: function (value, index) {
                    // 格式化成月/日，只在第一个刻度显示年份
            		if(value.length < 9){
            			return value;
            		}else{
            			var date = new Date(value);
	                    var texts = [(date.getMonth() + 1), date.getDate()];
	                    if (index === 0) {
	                        texts.unshift(date.getYear());
	                    }
	                    return texts.join('-');
            		}
                }
            }
		},
		yAxis : { //直角坐标系 grid 中的 y 轴
			type : 'value',
			axisLine:{
	        	show:false
	        },
            axisLabel : {
                formatter: '{value} %'
            }
		},
		series : [ //系列列表
		{	
			name : items['xName'],
			type : 'line',
			data : dataIntArr,
            markPoint: {
                data: [
                    {type: 'max', name: '最大值'},
                    {type: 'min', name: '最小值'}
                ]
            },
            markLine: {
                data: [
                	 /*[  自己制作的线		
                	        {name: '标线2起点', x: 80, y: 100},
                	        {name: '标线2终点', x: 720, y: 100}
                	]
                	,*/{type: 'average', name: '平均值'}
                ]
            }
		}]
	};
	myChart.setOption(option1); //参数设置方法     
}


//初始化echarts折线图
function initTowEcharts(items){
	// 基于准备好的dom，初始化echarts实例
	var myChart = echarts.init(document.getElementById(items['id']));
	var option1 = {
		title : {
		        text: items['xName'],
		        subtext: '间隔时间'+items['subtext']+'秒'
		},
		tooltip : { //提示框组件
			trigger : 'axis'
		},
		grid: {
        	x:'10%',
            x2: '5%',
            y: '20%',
            y2: '15%'
        },
        toolbox: {
            show : true,
            feature : {
                mark : {show: true},
                dataView : {show: true, readOnly: false},
                magicType : {show: true, type: ['line', 'bar']},
                restore : {show: true},
                saveAsImage : {show: true}
            }
        },
		xAxis : {
			type : 'category',
			boundaryGap : false,
			data : items['xData'],
			axisLine:{
	        	show:false
	        },
            axisLabel : {
            	formatter: function (value, index) {
                    // 格式化成月/日，只在第一个刻度显示年份
            		if(value.length < 9){
            			return value;
            		}else{
            			var date = new Date(value);
	                    var texts = [(date.getMonth() + 1), date.getDate()];
	                    if (index === 0) {
	                        texts.unshift(date.getYear());
	                    }
	                    return texts.join('-');
            		}
                }
            }
		},
		yAxis : { 
			type : 'value',
			axisLine:{
	        	show:false
	        },
            axisLabel : {
                formatter: '{value} %'
            }
		},
		series : eval('('+items['yData']+')')
	};
	myChart.setOption(option1); //参数设置方法     
}

//判断时间格式
function isTime(str)
{
	if(str.length!=5){
		return false;
	}
	var a = str.match(/^([01]\d|2[0-3]):([0-5]\d)$/);
	if (a == null) {return false;}
	if (a[1]>24 || a[2]>60)
	{
		return false
	}
	return true;
}


